/**
 * @(#)CodeExamples.java
 * read_From_File_Example
 * write_To_File_Example
 * idGenerator
 * @DS_092 
 * 
 */

import java.util.* ;
import java.io.* ;
public class CodeExamples{

   public static void main(String[] args)
   {
   		String fullText=get_File_String(".\\src\\Example2\\out2-4.txt").replaceAll("\\(|\\)|\\,", "");
   		System.out.println("Full & Trimmed file is:\n"+fullText);
	   int LZTextLength=fullText.length();
	System.out.println(LZTextLength*5);
		
	   String inputFile  = args[0]; 
	   String outputFile = args[1]; 
		
 	    read_From_File_Example(inputFile);
 	    write_To_File_Example(outputFile);
  				
   	
   }
   
   public static int get_File_Length(String inputFilename){
      	System.out.println("Reading from file " + inputFilename);
      	int ans=0;
      	try {
           File inFile = new File(inputFilename);
           FileReader ifr = new FileReader(inFile);
           String fullfile=ifr.toString();
           System.out.println("File length is "+fullfile.length()+"File string is \n"+fullfile);
           BufferedReader ibr= new BufferedReader(ifr) ;

           String line = "" ;
           while (line != null )
           {
               line = ibr.readLine() ;
               if (line != null)
               {
                   StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                   while(st.hasMoreTokens())
                   {
                   	String token = st.nextToken().trim();
                   	System.out.println(token);
                   	ans=ans+token.length();
                   }                   
               }
           }

           ibr.close();
           ifr.close();
       }

       catch(Exception e) {
           System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
           e.printStackTrace() ;
           System.exit(-1) ;      //brutally exit the program
       }
       return ans;
   }
   public static void read_From_File_Example(String inputFilename){
       	System.out.println("Reading from file " + inputFilename);
       
       	try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                    while(st.hasMoreTokens())
                    {
                    	String token = st.nextToken().trim();
                    	System.out.println(token);
                    }                   
                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }

   public static String get_File_String(String inputFilename){
      	System.out.println("Reading from file " + inputFilename);
      	String ans="";
      	try {
           File inFile = new File(inputFilename);
           FileReader ifr = new FileReader(inFile);
           String fullfile=ifr.toString();
           System.out.println("File length is "+fullfile.length()+"File string is \n"+fullfile);
           BufferedReader ibr= new BufferedReader(ifr) ;

           String line = "" ;
           while (line != null )
           {
               line = ibr.readLine() ;
               if (line != null)
               {
                   StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                   while(st.hasMoreTokens())
                   {
                   	String token = st.nextToken().trim();
                   	System.out.println(token);
                   	ans=ans+token;
                   }                   
               }
           }

           ibr.close();
           ifr.close();
       }

       catch(Exception e) {
           System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
           e.printStackTrace() ;
           System.exit(-1) ;      //brutally exit the program
       }
       return ans;
   }
    private static void write_To_File_Example(String outputFilename) {

        System.out.println("Writing to file " + outputFilename);
        
        
        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);

            //Writing to file
            ofw.write(" Hello World \n");
          	
            ofw.close();
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
    
 
}
    
  